package com.tau.iplayer.client.widgets;

import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.tau.iplayer.client.IPlayerUtils;
import com.tau.iplayer.client.models.Track;

public class ThumbnailTrackDisplay extends TrackDisplay {

	private static final int MAX_LENGTH = 50;

	public ThumbnailTrackDisplay(Track track) {
		super(track);
	}

	public void createTrackDisplay(Track track) {
		DockPanel panel = new DockPanel();
		Image thumbnail = new Image(track.getThumbnailURL(), 0, 10, 120, 70);
		panel.add(thumbnail, DockPanel.WEST);
		String dispStr = track.toString();
		dispStr = (dispStr.length() > MAX_LENGTH) ? dispStr.substring(0, MAX_LENGTH - 1) + "..."
				: dispStr;
		Grid grid = new Grid(2, 1);
		grid.setHeight("70px");
		grid.setWidth("100%");
		
		Label trackDispLabel = new Label(dispStr);
		trackDispLabel.setStyleName("standardLabel");
		trackDispLabel.setWidth("100%");
		grid.setWidget(0, 0, trackDispLabel);
		grid.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		
		Label durationLabel = new Label(IPlayerUtils.formatTime(track.getDuration()));
		durationLabel.setStyleName("standardBoldLabel");
		durationLabel.setWidth("100%");
		grid.setWidget(1, 0, durationLabel);
		grid.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_BOTTOM);
		grid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
		
		panel.add(grid, DockPanel.CENTER);
		panel.setCellWidth(grid, "100%");
		panel.setCellHorizontalAlignment(grid, HasHorizontalAlignment.ALIGN_LEFT);

		panel.setSpacing(3);

		initWidget(panel);
	}

}
